package com.mediocre.eg3publishsubscribe;

import com.mediocre.util.CommonUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

public class ReceiveLogs {

    public static final String EXCHANGE_NAME = "logs";

    public static void main(String[] args) throws IOException, TimeoutException {
        final Channel channel = CommonUtils.getChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        // when we supply no parameters to queueDeclare() we create a non-durable, exclusive, autodelete queue with a generated name
        String queueName = channel.queueDeclare().getQueue();
        // routingKey含义: Bindings can take an extra routingKey parameter. To avoid the confusion with a basic_publish parameter we're going to call it a binding key. This is how we could create a binding with a key:
        channel.queueBind(queueName, EXCHANGE_NAME, "");

        // channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
            System.out.println(" [x] received '" + message + "'");
        };
        // 不是手动提交确认消息，而是不自定确认 ？？？到底是什么意思，再确认下。
        boolean autoAck = false;
        channel.basicConsume(queueName, autoAck, deliverCallback, consumerTag -> {});
    }
}
